#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

int main (void)
{
	int shmid;
	pid_t pid;
	/*有亲缘关系的进程间的通信*/ 
	shmid = shmget (IPC_PRIVATE, 1024, IPC_CREAT |  IPC_EXCL | 0600);
	if(-1 == shmid) {
		perror ("shmget()");
		return -1;
	}
	pid = fork();
	if (-1 == pid) {
		perror ("fork()");
		goto ERROR;
	}
	if (0 == pid) {
		//子进程
		void *ptr = shmat (shmid, NULL, 0);
		memcpy (ptr, "can you see me?", 15);
		//解除映射
		shmdt (ptr);
		exit (0);
	}
	wait (NULL);
	//将共享内存映射到父进程的虚拟地址空间中
	void *ptr = shmat (shmid, NULL, 0);
	puts (ptr);
	//解除映射
	shmdt (ptr);
	//pause ();
	//销毁
	shmctl (shmid, IPC_RMID, NULL);
	return 0;
ERROR:
	shmctl (shmid, IPC_RMID, NULL);
}
